// aenpc.txt
// Like basicnpc, but can also project a damaging AE. 
// Memory Cells:
//   Cell 0 - If 0, default behavior. If 1, it doesnt fidget.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.
//   Cell 3 - Type of AE
//		0 - fire, does 1-6 * level
//		1 - cold, does 1-8 * level
//		2 - energy, does 1-10 * level
//		3 - slow
//		4 - debuff
//		5 - daze
//		6 - charm
//		7 - fear
//		8 - acid
//      9 - lightning aura
//		10 - poison
// 		11 - cleave (1-8 dam per level)
//		12 - acid + damage
//   Cell 4 - Range of AE, Left at default of 4 if 0
//   Cell 5 - Number of state when talked to. Plays default text if 0.
//   Cell 6 - % chance of using AE, defaults to 50% if left at 0

begincreaturescript;

variables;

short i,target;
short last_abil;
short ae_chance = 50;
short ae_range = 4;

body;

beginstate INIT_STATE;
	last_abil = get_current_tick();
	if (get_memory_cell(4) != 0)
		ae_range = get_memory_cell(4);
	if (get_memory_cell(6) != 0)
		ae_chance = get_memory_cell(6);
	break;

beginstate DEAD_STATE;
	if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
		inc_flag(get_memory_cell(1),get_memory_cell(2),1);
break;

beginstate START_STATE; 
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state(3);
			else set_foe_target(ME,-1);
		}
	
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	if (my_dist_from_start() >= 6) {
		if (get_ran(1,1,100) < 40) 
			return_to_start(ME,1);
		}
		else if (get_memory_cell(0) == 0)
			fidget(ME,25);

	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);

	if ((get_ran(1,0,100) < ae_chance) && (is_combat()) && 
	  (tick_difference(last_abil,get_current_tick()) > 0)) {
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();
	  	if (get_memory_cell(3) == 0) { // fire
		  	place_particle_num(ME,2,4,8);
			print_named_str(ME,"surrounds itself with a curtain of flame.");
			pc_heard_sound_delay(117,250);						
	  		create_missile_spiral(154,40,ae_range,2);
	  		damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,6),ae_range,2,0);
	  		}
	  	if (get_memory_cell(3) == 1) { // ice
		  	place_particle_num(ME,1,4,8);
			print_named_str(ME,"radiates a cloud of icy particles.");
			pc_heard_sound_delay(165,250);						
	  		create_missile_spiral(151,40,ae_range,2);
	  		damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,8),ae_range,6,0);
	  		}
	  	if (get_memory_cell(3) == 2) { // energy
		  	place_particle_num(ME,1,4,8);
			print_named_str(ME,"radiates a cloud of searing energy.");
			pc_heard_sound_delay(179,250);						
	  		create_missile_spiral(155,40,ae_range,2);
	  		damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,10),ae_range,1,0);
	  		}
	  	if (get_memory_cell(3) == 3) { // slow
		  	place_particle_num(ME,3,6,8);
			print_named_str(ME,"cast a slowing spell.");
			pc_heard_sound_delay(136,250);						
			status_nearby(-6,ae_range,1,0);
	  		}
	  	if (get_memory_cell(3) == 4) { // debuff
		  	place_particle_num(ME,3,6,8);
			spray_missiles(43,8,ae_range);
			print_named_str(ME,"radiates an aura of static.");
			pc_heard_sound_delay(201,250);						
			particle_nearby(ae_range,8,2,4,2);
			status_nearby(-10,ae_range,8,0);
			status_nearby(-15,ae_range,9,0);
			status_nearby(-15,ae_range,11,0);
			status_nearby(-15,ae_range,12,0);
			status_nearby(-15,ae_range,13,0);
			status_nearby(-15,ae_range,14,0);
			status_nearby(-15,ae_range,15,0);
			status_nearby(-15,ae_range,16,0);
			status_nearby(-15,ae_range,18,0);
			status_nearby(-15,ae_range,19,0);
	  		}
	  	if (get_memory_cell(3) == 5) { // daze
		  	place_particle_num(ME,3,6,8);
			print_named_str(ME,"emits a dazing aura.");
			pc_heard_sound_delay(136,250);						
			status_nearby(get_level(ME) + 5 + get_attack_bonus(ME),ae_range,17,0);
	  		}
	  	if (get_memory_cell(3) == 6) { // charm
		  	place_particle_num(ME,0,6,8);
			print_named_str(ME,"emits a strange, wonderful scent.");
			pc_heard_sound_delay(100,250);						
			status_nearby(get_level(ME) + 5 + get_attack_bonus(ME),ae_range,5,0);
	  		}
	  	if (get_memory_cell(3) == 7) { // fear
		  	place_particle_num(ME,4,6,8);
			print_named_str(ME,"emits an aura of terror.");
			pc_heard_sound_delay(107,250);						
			status_nearby(get_level(ME) + 5 + get_attack_bonus(ME),ae_range,7,0);
	  		}
	  	if (get_memory_cell(3) == 8) { // acid
		  	place_particle_num(ME,4,6,8);
			print_named_str(ME,"emits an acid spray.");
			pc_heard_sound_delay(177,250);			
	  		create_missile_spiral(142,40,ae_range,2);
			status_nearby(get_level(ME) + 5 + get_attack_bonus(ME),ae_range,6,0);
	  		}
	  	if (get_memory_cell(3) == 9) { // lightning aura
		  	place_particle_num(ME,1,4,8);
			print_named_str(ME,"surrounds itself with an aura of lightning.");
			pc_heard_sound_delay(179,250);						
	  		create_missile_spiral(147,40,ae_range,2);
	  		damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,5),ae_range,1,0);
			status_nearby(get_level(ME) / 4,ae_range,20,0);
	  		}
	  	if (get_memory_cell(3) == 10) { // poison
		  	place_particle_num(ME,4,6,8);
			print_named_str(ME,"emits a cloud of poison gas.");
			pc_heard_sound_delay(177,250);			
	  		create_missile_spiral(142,40,ae_range,2);
			status_nearby(get_level(ME) + 5 + get_attack_bonus(ME),ae_range,2,0);
	  		}
		if (get_memory_cell(3) == 11) { // cleave
			print_named_str(ME,"spins around, cleaving the air with a mighty blow.");
			damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,8),ae_range,0,0);
			pc_heard_sound_delay(3,100);				
			spray_missiles(15,12,ae_range);
			}
	  	if (get_memory_cell(3) == 12) { // acid + damage
		  	place_particle_num(ME,4,6,8);
			print_named_str(ME,"emits an acid spray.");
			pc_heard_sound_delay(177,250);			
	  		create_missile_spiral(142,40,ae_range,2);
			status_nearby(get_level(ME) + 5 + get_attack_bonus(ME),ae_range,6,0);
	  		damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,8),ae_range,5,0);
	  		}

		end();
		}
		
	do_attack();
break;

beginstate TALKING_STATE;
	if (get_memory_cell(5) == 0) {
		print_str("Talking: You make a bit of small talk, but you don't learn");
		print_str("  anything interesting.");
		}
		else begin_talk_mode(get_memory_cell(5));
break;